export function uploadImage(file) {
  // 文件类型校验（与后端保持一致）
  const allowedTypes = ['image/jpeg', 'image/png', 'image/gif', 'image/jpg']
  if (!allowedTypes.includes(file.type)) {
    return Promise.reject(new Error('仅支持 JPG/JPEG/PNG/GIF 格式'))
  }

  return new Promise((resolve, reject) => {
    uni.uploadFile({
      url: 'http://localhost:3000/upload/image',
      filePath: file.path,
      name: 'file',
      formData: {
        'Content-Type': 'multipart/form-data',
      },
      success: (res) => {
        if (res.statusCode === 200) {
          const data = JSON.parse(res.data)
          resolve(`http://localhost:3000${data.url}`)
        }
        else {
          reject(new Error('上传失败'))
        }
      },
      fail: (err) => {
        reject(err)
      },
      complete: (res) => {
        console.log('上传完成', res)
      },
    })
  })
}
